<?php

namespace App\Http\Controllers\Admin;
header('Content-Type:text/html;charset=utf-8');


use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Route,URL,Auth;

class MainapiController extends Controller
{
	public function index(request $request){
		//获取重点单位

		//接受传过来的areaid
		//2为$_POST['id']

    	if ($request->id) {
    		$narid = $request->id;
    		$aid['id'] = $request->id;
    	}else{
    		$narid = 1;
    		$aid['id'] = 1;
    	}




    	$area = DB::table('area')->where($aid)->first();
    		if ($area->dengji == 1) {

			//开始获取重点单位
			$re = DB::table('units')->where([
			    ['city', '=', $narid],
			    ['is_imp', '<>', '40'],
			])->orderBy('id','desc')->get();

			//获取重要部位
			$zybw = DB::select("SELECT zy.id,zy.name,units.name as gname FROM units INNER JOIN zhongyaobuwei as zy on units.id = zy.unit_id WHERE (units.city = $narid) GROUP BY zy.id,zy.name,gname");

			//获取重点人员
			$zdry = DB::select("SELECT zr.id,zr.name,units.name as gname FROM units INNER JOIN zhongdianrenyuan as zr on units.id = zr.unit_id WHERE (units.city = $narid) GROUP BY zr.id,zr.name,gname");


			//治安保卫重点单位统计表（总数）
			$tj = DB::select("SELECT units.type,area.name,unit_type.name as typename,COUNT(*) as count FROM units  LEFT JOIN area on units.city = area.id  LEFT JOIN unit_type on units.type = unit_type.id WHERE (units.city = $narid and units.is_imp != 0) GROUP BY units.type,area.name,unit_type.name");


		}

		if ($area->dengji == 2) {
			//开始获取重点单位
			$re = DB::table('units')->where([
			    ['area', '=', $narid],
			    ['is_imp', '<>', '40'],
			])->orderBy('id','desc')->get();

			//获取重要部位
			$zybw = DB::select("SELECT zy.id,zy.name,units.name as gname FROM units INNER JOIN zhongyaobuwei as zy on units.id = zy.unit_id WHERE (units.area = $narid) GROUP BY zy.id,zy.name,gname");

			//获取重点人员
			$zdry = DB::select("SELECT zr.id,zr.name,units.name as gname FROM units INNER JOIN zhongdianrenyuan as zr on units.id = zr.unit_id WHERE (units.area = $narid) GROUP BY zr.id,zr.name,gname");




    		//治安保卫重点单位统计表（总数）
			$tj = DB::select("SELECT units.type,area.name,unit_type.name as typename,COUNT(*) as count FROM units  LEFT JOIN area on units.city = area.id  LEFT JOIN unit_type on units.type = unit_type.id WHERE (units.area = $narid and units.is_imp != 0) GROUP BY units.type,area.name,unit_type.name");
		}

		if ($area->dengji == 3) {
			//开始获取重点单位
			$re = DB::table('units')->where([
			    ['org_id', '=', $narid],
			    ['is_imp', '<>', '40'],
			])->orderBy('id','desc')->get();


			$zybw = DB::select("SELECT zy.id,zy.name,units.name as gname FROM units INNER JOIN zhongyaobuwei as zy on units.id = zy.unit_id WHERE (units.org_id = $narid) GROUP BY zy.id,zy.name,gname");


			//获取重点人员
			$zdry = DB::select("SELECT zr.id,zr.name,units.name as gname FROM units INNER JOIN zhongdianrenyuan as zr on units.id = zr.unit_id WHERE (units.org_id = $narid) GROUP BY zr.id,zr.name,gname");


			//治安保卫重点单位统计表（总数）
			$tj = DB::select("SELECT units.type,area.name,unit_type.name as typename,COUNT(*) as count FROM units  LEFT JOIN area on units.city = area.id  LEFT JOIN unit_type on units.type = unit_type.id WHERE (units.org_id = $narid and units.is_imp != 0) GROUP BY units.type,area.name,unit_type.name");

		}
		$str = '';
		$num = count($re);
		foreach($re as $k=>$v){
			$str .= '<div class="cf" onclick="deletePoint('.($num-($k-1)).')"><div class="col-l"><div class="row name">'.$v->name.'</div><div class="row">法人名称：'.$v->frdb_name.'</div><div class="row tel">联系方式：'.$v->frdb_phone.'</div></div><div class="col-r"><img src="http://nb.yntosite.com/'.$v->thumb_pic.'"></div></div>';
		}

		//$result['total'] = $re->count();
		$result['rows'] = $re;
		$zddw = json_encode($str,true);
		echo($zddw);

		//获取重点单位




	}

	public function indextwo(request $request){
		//获取重点单位

		//接受传过来的areaid
		//2为$_POST['id']

    		if ($request->id) {
    		$narid = $request->id;
    		$aid['id'] = $request->id;
    	}else{
    		$narid = 1;
    		$aid['id'] = 1;
    	}




    	$area = DB::table('area')->where($aid)->first();
    		if ($area->dengji == 1) {

			//开始获取重点单位
			$re = DB::table('units')->where([
			    ['city', '=', $narid],
			    ['is_imp', '<>', '0'],
			])->orderBy('id','desc')->get();

			//获取重要部位
			$zybw = DB::select("SELECT zy.id,zy.name,units.name as gname FROM units INNER JOIN zhongyaobuwei as zy on units.id = zy.unit_id WHERE (units.city = $narid) GROUP BY zy.id,zy.name,gname");

			//获取重点人员
			$zdry = DB::select("SELECT zr.id,zr.name,units.name as gname FROM units INNER JOIN zhongdianrenyuan as zr on units.id = zr.unit_id WHERE (units.city = $narid) GROUP BY zr.id,zr.name,gname");


			//治安保卫重点单位统计表（总数）
			$tj = DB::select("SELECT units.type,area.name,unit_type.name as typename,COUNT(*) as count FROM units  LEFT JOIN area on units.city = area.id  LEFT JOIN unit_type on units.type = unit_type.id WHERE (units.city = $narid and units.is_imp != 0) GROUP BY units.type,area.name,unit_type.name");


		}

		if ($area->dengji == 2) {
			//开始获取重点单位
			$re = DB::table('units')->where([
			    ['area', '=', $narid],
			    ['is_imp', '<>', '0'],
			])->orderBy('id','desc')->get();

			//获取重要部位
			$zybw = DB::select("SELECT zy.id,zy.name,units.name as gname FROM units INNER JOIN zhongyaobuwei as zy on units.id = zy.unit_id WHERE (units.area = $narid) GROUP BY zy.id,zy.name,gname");

			//获取重点人员
			$zdry = DB::select("SELECT zr.id,zr.name,units.name as gname FROM units INNER JOIN zhongdianrenyuan as zr on units.id = zr.unit_id WHERE (units.area = $narid) GROUP BY zr.id,zr.name,gname");




    		//治安保卫重点单位统计表（总数）
			$tj = DB::select("SELECT units.type,area.name,unit_type.name as typename,COUNT(*) as count FROM units  LEFT JOIN area on units.city = area.id  LEFT JOIN unit_type on units.type = unit_type.id WHERE (units.area = $narid and units.is_imp != 0) GROUP BY units.type,area.name,unit_type.name");
		}

		if ($area->dengji == 3) {
			//开始获取重点单位
			$re = DB::table('units')->where([
			    ['org_id', '=', $narid],
			    ['is_imp', '<>', '0'],
			])->orderBy('id','desc')->get();


			$zybw = DB::select("SELECT zy.id,zy.name,units.name as gname FROM units INNER JOIN zhongyaobuwei as zy on units.id = zy.unit_id WHERE (units.org_id = $narid) GROUP BY zy.id,zy.name,gname");


			//获取重点人员
			$zdry = DB::select("SELECT zr.id,zr.name,units.name as gname FROM units INNER JOIN zhongdianrenyuan as zr on units.id = zr.unit_id WHERE (units.org_id = $narid) GROUP BY zr.id,zr.name,gname");


			//治安保卫重点单位统计表（总数）
			$tj = DB::select("SELECT units.type,area.name,unit_type.name as typename,COUNT(*) as count FROM units  LEFT JOIN area on units.city = area.id  LEFT JOIN unit_type on units.type = unit_type.id WHERE (units.org_id = $narid and units.is_imp != 0) GROUP BY units.type,area.name,unit_type.name");

		}


		//$result['total'] = $re->count();
		$result['rows'] = $zybw;
		$zddw = json_encode($result,true);
		echo($zddw);



		//获取重点单位




	}


	public function indexthree(request $request){
		//获取重点单位

		//接受传过来的areaid
		//2为$_POST['id']
		if ($request->id) {
    		$narid = $request->id;
    		$aid['id'] = $request->id;
    	}else{
    		$narid = 1;
    		$aid['id'] = 1;
    	}


    	$area = DB::table('area')->where($aid)->first();
    		if ($area->dengji == 1) {

			//开始获取重点单位
			$re = DB::table('units')->where([
			    ['city', '=', $narid],
			    ['is_imp', '<>', '0'],
			])->orderBy('id','desc')->get();

			//获取重要部位
			$zybw = DB::select("SELECT zy.id,zy.name,units.name as gname FROM units INNER JOIN zhongyaobuwei as zy on units.id = zy.unit_id WHERE (units.city = $narid) GROUP BY zy.id,zy.name,gname");

			//获取重点人员
			$zdry = DB::select("SELECT zr.id,zr.name,units.name as gname FROM units INNER JOIN zhongdianrenyuan as zr on units.id = zr.unit_id WHERE (units.city = $narid) GROUP BY zr.id,zr.name,gname");


			//治安保卫重点单位统计表（总数）
			$tj = DB::select("SELECT units.type,area.name,unit_type.name as typename,COUNT(*) as count FROM units  LEFT JOIN area on units.city = area.id  LEFT JOIN unit_type on units.type = unit_type.id WHERE (units.city = $narid and units.is_imp != 0) GROUP BY units.type,area.name,unit_type.name");


		}

		if ($area->dengji == 2) {
			//开始获取重点单位
			$re = DB::table('units')->where([
			    ['area', '=', $narid],
			    ['is_imp', '<>', '0'],
			])->orderBy('id','desc')->get();

			//获取重要部位
			$zybw = DB::select("SELECT zy.id,zy.name,units.name as gname FROM units INNER JOIN zhongyaobuwei as zy on units.id = zy.unit_id WHERE (units.area = $narid) GROUP BY zy.id,zy.name,gname");

			//获取重点人员
			$zdry = DB::select("SELECT zr.id,zr.name,units.name as gname FROM units INNER JOIN zhongdianrenyuan as zr on units.id = zr.unit_id WHERE (units.area = $narid) GROUP BY zr.id,zr.name,gname");




    		//治安保卫重点单位统计表（总数）
			$tj = DB::select("SELECT units.type,area.name,unit_type.name as typename,COUNT(*) as count FROM units  LEFT JOIN area on units.city = area.id  LEFT JOIN unit_type on units.type = unit_type.id WHERE (units.area = $narid and units.is_imp != 0) GROUP BY units.type,area.name,unit_type.name");
		}

		if ($area->dengji == 3) {
			//开始获取重点单位
			$re = DB::table('units')->where([
			    ['org_id', '=', $narid],
			    ['is_imp', '<>', '0'],
			])->orderBy('id','desc')->get();


			$zybw = DB::select("SELECT zy.id,zy.name,units.name as gname FROM units INNER JOIN zhongyaobuwei as zy on units.id = zy.unit_id WHERE (units.org_id = $narid) GROUP BY zy.id,zy.name,gname");


			//获取重点人员
			$zdry = DB::select("SELECT zr.id,zr.name,units.name as gname FROM units INNER JOIN zhongdianrenyuan as zr on units.id = zr.unit_id WHERE (units.org_id = $narid) GROUP BY zr.id,zr.name,gname");


			//治安保卫重点单位统计表（总数）
			$tj = DB::select("SELECT units.type,area.name,unit_type.name as typename,COUNT(*) as count FROM units  LEFT JOIN area on units.city = area.id  LEFT JOIN unit_type on units.type = unit_type.id WHERE (units.org_id = $narid and units.is_imp != 0) GROUP BY units.type,area.name,unit_type.name");

		}


		//$result['total'] = $re->count();
		$result['rows'] = $zdry;
		$zddw = json_encode($result,true);
		echo($zddw);







	}







}
